package terra.model;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

import terra.Config;
import terra.definitions.item.ItemDef;
import terra.managers.DefManager;
import terra.managers.LightManager;
import terra.managers.ResourceManager;
import terra.model.visual.AbstractVisualizator;
import terra.model.visual.AnimationVisualizator;
import terra.model.visual.LRImageVisualizator;
import terra.slick.GameState;

public class LightObject extends GameObject {

	public Light light;
	public boolean lights = true;
	private float radius;
	boolean add = false;
	int addTime = 1000;
	int rndAddTime = 1000;
	AbstractVisualizator animation;
	
	public void init(){
		rndAddTime = 500+GameState.rnd.nextInt(500);
		ItemDef def = DefManager.allItems.get(getId());
		Color col = new Color(def.getIntProperty("lightColorRed"),
				def.getIntProperty("lightColorGreen"),
				def.getIntProperty("lightColorBlue"));
		light = new Light(col, getX(),getY(),radius);
		initRender();
		LightManager.getInstance().getLights().add(light);
	}
	
	public void update(int delta){
		if (lights)
			animation.update(delta);
//		addTime-=delta;
//		if (add)
//			light.setRadius(light.getRadius() + 0.02f);
//		else
//			light.setRadius(light.getRadius() - 0.02f);
//		if (addTime < 0){
//			//System.out.println(this +" updated");
//			addTime = rndAddTime;
//			add = !add;
//			if (add)
//				light.setRadius(light.getRadius() + 1);
//			else
//				light.setRadius(light.getRadius() - 1);
//		}
	}
	
	
	public void render(Graphics g){
		if (!lights)
			render.draw(g, this, getX(), getY());
		else
			animation.draw(g, this, getX(), getY());
	}	
	
	@Override
	public void setX(float xx){
		super.setX(xx);
		if (light != null)
			light.setCenterX(xx);
	}
	
	@Override
	public void setY(float xx){
		super.setY(xx);
		if (light != null)
			light.setCenterY(xx);
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	public void setLights(boolean b) {
		if (!lights && b)
			LightManager.getInstance().getLights().add(light);
		else if (lights && !b)
			LightManager.getInstance().getLights().remove(light);
		lights = b;
	}
	
	public void initRender()
	{
		String image = DefManager.allItems.get(getId()).image;
		LRImageVisualizator render = new LRImageVisualizator();
		render.renderName = image;
		render.render = ResourceManager.getImage(image);
		render.render2 = ResourceManager.getImage(image).getFlippedCopy(true, false);
		render.blend = true;
		render.setSize(Config.tileSize, Config.tileSize);
		this.render = render;
		
		AnimationVisualizator render2 = new AnimationVisualizator();
		render2.renderName = image;
		render2.render = ResourceManager.getAnimation(image);
		render2.blend = false;
		render2.setSize(Config.tileSize, Config.tileSize);
		animation = render2;
	}
}
